package com.ys.service.mall.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bo.vo.R;
import com.ys.entity.flow.YspModel;
import com.ys.entity.mall.TmGift;
import com.ys.entity.mall.TmGiftGiven;
import com.ys.entity.mall.TmGiftUse;
import com.ys.entity.sys.TsUserInfo;
import com.ys.entity.sys.YsMenu;
import com.ys.enume.flow.FlowEnum;
import com.ys.enume.mall.TmGiftEnum;
import com.ys.mapper.mall.TmGiftGivenMapper;
import com.ys.mapper.mall.TmGiftMapper;
import com.ys.mapper.mall.TmGiftUseMapper;
import com.ys.service.flow.YspTaskService;
import com.ys.service.mall.TmGiftService;
import com.ys.util.shiro.ShiroUtils;

@Service("tmGiftService")
public class TmGiftServiceImpl implements TmGiftService {
	
	@Autowired
	private TmGiftMapper tmGiftMapper;
	@Autowired
	private TmGiftGivenMapper tmGiftGivenMapper;
	@Autowired
	private TmGiftUseMapper tmGiftUseMapper;
	
	@Autowired
	private YspTaskService yspTaskService;
	
	@Override
	@Transactional
	public R use(String bid, String code, String address, Long menuId) throws Exception {

		if (bid == null || bid.length() == 0) {
			return R.error("单号不能为空");
		} else if (new TmGiftUse().setBid(bid).count() > 0) {
			return R.error("单号已存在");
		}
		
		if (code == null) {
			return R.error("礼品编码不存在");
		}
		TmGift tmGift = new TmGift().setCode(code).setState(TmGiftEnum.State.ABLE.value).queryFirst();
		if (tmGift == null) {
			return R.error("礼品不存在");
		}
		
		// 更新礼品
		tmGift.setState(TmGiftEnum.State.DISABLE.value);
		tmGiftMapper.update(tmGift);
		
		// 添加礼品使用审核
		TmGiftUse entity = null;
		{
			String codeGift = tmGift.getCode();
			Long goodsId = tmGift.getGoodsId();
			String goodsCode = tmGift.getGoodsCode();
			String goodsName = tmGift.getGoodsName();
			Integer effectStock = FlowEnum.EffectStock.NOT_EFFECT.value;
			String createUser = tmGift.getUserId();
			String remark = "测试备注说明";
			entity = TmGiftUse.tmGiftUse(bid, codeGift, goodsId, goodsCode, goodsName, address, createUser, effectStock, remark, 1);
			tmGiftUseMapper.insert(entity);
		}
		

		YsMenu ysMenu = new YsMenu().setId(menuId).queryFirst();
		if (ysMenu.getFlowModelId().longValue() != 0L) {
			YspModel yspModel = new YspModel().setId(ysMenu.getFlowModelId()).queryFirst();
			String codeModel = yspModel.getCode();
			String createUserId = ShiroUtils.getUserId();
			yspTaskService.init(codeModel, bid, createUserId);
		}
		
		return R.ok("操作成功");
	}
	
	@Override
	@Transactional
	public R send(String bid, String code, String userIdTo, Long menuId) throws Exception {

		if (bid == null || bid.length() == 0) {
			return R.error("单号不能为空");
		} else if (new TmGiftGiven().setBid(bid).count() > 0) {
			return R.error("单号已存在");
		}
		
		if (code == null) {
			return R.error("礼品编码不存在");
		}
		TmGift tmGift = new TmGift().setCode(code).setState(TmGiftEnum.State.ABLE.value).queryFirst();
		if (tmGift == null) {
			return R.error("礼品不存在");
		}
		
		// 验证接收人
		if (new TsUserInfo().setUserId(userIdTo).count() == 0) {
			return R.error("接收人工号有误");
		}
		
		// 更新礼品
		tmGift.setState(TmGiftEnum.State.DISABLE.value);
		tmGiftMapper.update(tmGift);
		
		// 添加礼品转赠审核
		TmGiftGiven entity = null;
		{
			String userGiftCode = tmGift.getCode();
			Long goodsId = tmGift.getGoodsId();
			String goodsCode = tmGift.getGoodsCode();
			String goodsName = tmGift.getGoodsName();
			Integer effectStock = FlowEnum.EffectStock.NOT_EFFECT.value;
			String createUser = tmGift.getUserId();
			entity = TmGiftGiven.tmGiftGiven(bid, effectStock, userGiftCode, goodsId, goodsCode, goodsName, userIdTo, createUser, 1);
			tmGiftGivenMapper.insert(entity);
		}
		

		YsMenu ysMenu = new YsMenu().setId(menuId).queryFirst();
		if (ysMenu.getFlowModelId().longValue() != 0L) {
			YspModel yspModel = new YspModel().setId(ysMenu.getFlowModelId()).queryFirst();
			String codeModel = yspModel.getCode();
			String createUserId = ShiroUtils.getUserId();
			yspTaskService.init(codeModel, bid, createUserId);
		}
		
		return R.ok("操作成功");
	}
}
